#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=100001;
int n,m;
vector<int> adj[MAXN];
struct node{
	int ls,rs;
	ll s,sl,sr;
}t[MAXN*20];
int tot,rt[MAXN];
void copy(int &p){
	t[++tot]=t[p];
	p=tot;
}
void insert(int &p,int l,int r,int x,int y){
	copy(p);
	t[p].s++,t[p].sl+=x,t[p].sr+=y;
	if(l==r)return;
	int mid=l+r>>1;
	if(x<=mid)insert(t[p].ls,l,mid,x,y);
	else insert(t[p].rs,mid+1,r,x,y);
}
ll query(int p,int l,int r,int x,int y,int a,int b,int c){
	if(!p)return 0;
	if(x<=l&&r<=y)return t[p].s*a+t[p].sl*b+t[p].sr*c;
	int mid=l+r>>1;
	ll v=0;
	if(x<=mid)v+=query(t[p].ls,l,mid,x,y,a,b,c);
	if(mid<y)v+=query(t[p].rs,mid+1,r,x,y,a,b,c);
	return v;
}
ll get(int ll,int rl,int lr,int rr,int a,int b,int c){
	if(ll>rl||lr>rr)return 0;
	return query(rt[rr],1,n,ll,rl,a,b,c)-query(rt[lr-1],1,n,ll,rl,a,b,c);
}
ll f[MAXN];
ll w(int l,int r){
	ll v=f[l++];
	v+=get(1,l,r,n,1,0,0);
	v+=get(l,r,l,r,r-l,1,-1);
	int mid=(l+r+1>>1)-1;
	v+=get(1,l-1,l,mid,-l+1,0,1);
	v+=get(1,l-1,mid+1,r,r,0,-1);
	mid=l+r>>1;
	v+=get(l,mid,r+1,n,-l,1,0);
	v+=get(mid+1,r,r+1,n,r+1,-1,0);
	return v;
}
struct best{
	int l,r,d;
}q[MAXN];
int hd,tl;
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	freopen("block.in","r",stdin);
	freopen("block.out","w",stdout);
	cin>>n>>m;
	n--;
	for(int i=1;i<=m;i++){
		int l,r;
		cin>>l>>r;
		adj[r-1].push_back(l);
	}
	for(int i=1;i<=n;i++){
		rt[i]=rt[i-1];
		for(int j:adj[i])insert(rt[i],1,n,j,i);
	}
	hd=1,tl=1;
	q[1]=(best){1,n,0};
	for(int i=1;i<=n;i++){
		while(q[hd].r<i)hd++;
		f[i]=w(q[hd].d,i);
		if(i==n)break;
		if(q[hd].r==i)hd++;
		q[hd].l=i+1;
		int pl=n+1;
		while(hd<=tl&&w(i,q[tl].l)<w(q[tl].d,q[tl].l))pl=q[tl--].l;
		if(hd<=tl){
			int l=q[tl].l,r=q[tl].r+1;
			while(l<r){
				int mid=l+r>>1;
				if(w(i,mid)<w(q[tl].d,mid))r=mid;
				else l=mid+1;
			}
			pl=l;
			q[tl].r=pl-1;
		}
		if(pl<=n)q[++tl]=(best){pl,n,i};
	}
	cout<<f[n];
	return 0;
}
